/*
 *  Copyright 2007 pollux.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.poxd.frisar.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.util.Date;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import org.poxd.frisar.Frisar;
import org.poxd.frisar.model.FriseElement;
import org.poxd.frisar.model.Period;

/**
 *
 * 
 */
public class DefaultPeriodRenderer extends JComponent implements FriseComponentRenderer {

    private Frisar frise;
    private boolean isSelected;
    private long start,  end;
    private int width,  height;
    private JTextArea text;

    public DefaultPeriodRenderer() {
        text = new JTextArea();
        text.setLineWrap(true);
        text.setWrapStyleWord(true);
    }

    public JComponent getFriseComponentRenderer(Frisar frise, FriseElement value, boolean isSelected) {
        this.frise = frise;
        Period period = (Period) value; //It's a PERIOD renderer
        start = period.getStart();
        end = period.getEnd();
        width = (int) (end - start) * 6; //will be replaced by a calculated unit
//      height = frise.getHeight();
        height = 75; // will be define by a frisar property
        this.isSelected = isSelected;
        
        text.setOpaque(false);
        text.setText(period.getTitle());
        Dimension dim = new Dimension(width - 10, height -10);
        text.setSize(dim);
        text.setMaximumSize(dim);
        text.setPreferredSize(dim);
        return this;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g.create();
        Rectangle2D.Double period = new Rectangle2D.Double(0, 0, width, height);
        GradientPaint p1 = new GradientPaint(0, 0, new Color(0, 0, 0), 0,
                height - 1, new Color(100, 100, 100));

        g2.setPaint(p1);
        g2.draw(period);
        g2.setColor(Color.lightGray);
        g2.fill(period);
        g2.dispose();
        
//        g.translate(5, 5);
        text.paint(g);
        g.dispose();
    }

    @Override
    public Dimension getMinimumSize() {
        return getPreferredSize();
    }

    @Override
    public Dimension getMaximumSize() {
        return getPreferredSize();
    }

    @Override
    public Dimension getSize() {
        return getPreferredSize();
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(width, height);
    }
    
    

    public static void main(String... args) {
        JFrame f = new JFrame();
        f.setSize(400, 400);
        DefaultPeriodRenderer r = new DefaultPeriodRenderer();
        f.add(r.getFriseComponentRenderer(null, new Period("Période test très très très très très longue...", new Date().getTime(), new Date().getTime() + 10), false));
        f.pack();
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
